#ifndef TCPMGR_H
#define TCPMGR_H
#include "global.h"
#include "singleton.h"
#include "QTcpServer"
#include "searchdata.h"

class TcpMgr :public QObject, public Singleton<TcpMgr>,public std::enable_shared_from_this<TcpMgr>
{
    Q_OBJECT
public:
    ~TcpMgr();
private:
    friend class Singleton<TcpMgr>;
    TcpMgr();
    QTcpSocket _socket;
    QString _host;
    uint16_t _port;

    void initHandles();
    QMap<ReqId,std::function<void(ReqId id, int len, QByteArray data)>> _handles;

    bool _is_pending;
    quint16 _head_id;
    quint16 _head_len;
    QByteArray _buffer;
    void handleMsg(ReqId id,int len,QByteArray& data);

signals:
    void sig_switch_chatdlg();
    void sig_con_success(bool scon);
    void sig_send_data(ReqId id,QByteArray data);
    void sig_login_fail(int err);
    void sig_user_search_fail(int err);
    void sig_user_search(std::shared_ptr<SearchInfo> si);  //查找好友后search_list加载好友
    void sig_friend_apply(std::shared_ptr<AddFriendApply>);//申请添加好友后的信号
    void sig_add_auth_friend(std::shared_ptr<AuthInfo>);   //收到对面同意好友申请后对面回包的信号
    void sig_auth_rsp(std::shared_ptr<AuthRsp>);           //收到对面好友申请，然后同意的信号
    void sig_text_chat_msg(std::shared_ptr<TextChatMsg>);
public slots:
    void slot_tcp_connect(ServerInfo si);
    void slot_send_data(ReqId id,QByteArray data);
};

#endif // TCPMGR_H
